<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item label-width="120" :label-position="labelPosition" label="微信号" prop="wechatNumber">
				<u-input :border="border" placeholder="请输入微信号" v-model="model.wechatNumber" type="text"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="性别" prop="sex">
				<u-input :border="border" type="select" :select-open="actionSheetShow" v-model="model.sex"
					placeholder="请选择性别" @click="actionSheetShow = true"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="所在地区" prop="region" label-width="150">
				<u-input :border="border" type="select" :select-open="pickerShow" v-model="model.region"
					placeholder="请选择地区" @click="pickerShow = true"></u-input>
			</u-form-item>

			<u-form-item :label-position="labelPosition" label="上传图片" prop="photo" label-width="150">
				<u-upload width="160" height="160"></u-upload>
			</u-form-item>
		</u-form>

		<u-button @click="submit">提交</u-button>
		<u-action-sheet :list="actionSheetList" v-model="actionSheetShow" @click="actionSheetCallback"></u-action-sheet>
		<u-picker mode="region" v-model="pickerShow" @confirm="regionConfirm"></u-picker>

	</view>
</template>

<script>
	import API from '@/api/api.js';
	export default {
		data() {
			let that = this;
			return {
				model: {
					wechatNumber: '',
					sex: '',
					sexCode: '',
					photo: '',
					region: '',
					userId: '',
				},

				rules: {
					wechatNumber: [{
						required: true,
						message: '请输入微信号',
						trigger: 'blur',
					}],
					sex: [{
						required: true,
						message: '请选择性别',
						trigger: 'change'
					}, ],

					region: [{
						required: true,
						message: '请选择地区',
						trigger: 'change',
					}],

				},
				border: false,
				check: false,
				selectStatus: 'close',
				actionSheetList: [{
						code: '1',
						text: '男'
					},
					{
						code: '2',
						text: '女'
					}
				],
				actionSheetShow: false,
				pickerShow: false,
				selectShow: false,
				radioCheckWidth: 'auto',
				radioCheckWrap: false,
				labelPosition: 'left',
				codeTips: '',
				errorType: ['message'],
			};
		},
		onLoad:function(option){
			console.log(option)
			this.model.userId=option.userId;
		},
		computed: {
			borderCurrent() {
				return this.border ? 0 : 1;
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						var data = JSON.stringify(this.model)
						API.saveSpot(data)
							.then(res => {
								// this.$u.toast('添加成功');
								uni.navigateTo({
									url: "/pages/index/index"
								})
							})
							.catch(err => {
								console.log(err);
							});
					} else {
						console.log('验证失败');
					}
				});
			},
			// 点击actionSheet回调
			actionSheetCallback(index) {
				uni.hideKeyboard();
				this.model.sex = this.actionSheetList[index].text;
				this.model.sexCode = this.actionSheetList[index].code;
			},

			// radio选择发生变化
			radioGroupChange(e) {
				this.model.payType = e;
			},

			// 选择地区回调
			regionConfirm(e) {
				this.model.region = e.province.label + '-' + e.city.label + '-' + e.area.label;
			},

			borderChange(index) {
				this.border = !index;
			},
			radioCheckboxChange(index) {
				if (index == 0) {
					this.radioCheckWrap = false;
					this.radioCheckWidth = 'auto';
				} else if (index == 1) {
					this.radioCheckWrap = true;
					this.radioCheckWidth = 'auto';
				} else if (index == 2) {
					this.radioCheckWrap = false;
					this.radioCheckWidth = '50%';
				}
			},
			labelPositionChange(index) {
				this.labelPosition = index == 0 ? 'left' : 'top';
			},
			codeChange(text) {
				this.codeTips = text;
			},

			errorChange(index) {
				if (index == 0) this.errorType = ['message'];
				if (index == 1) this.errorType = ['toast'];
				if (index == 2) this.errorType = ['border-bottom'];
				if (index == 3) this.errorType = ['border'];
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;

		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}
</style>
